package org.freeone.oauth2.authorizeserver.service.impl;

import org.freeone.oauth2.authorizeserver.dao.ClientRepository;
import org.freeone.oauth2.authorizeserver.entity.ClientEntity;
import org.freeone.oauth2.authorizeserver.service.AuthorizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class AuthorizeServiceImpl implements AuthorizeService {


    @Autowired
    private ClientRepository clientRepository;

    /**
     * 根据客户端id 查询客户端是否存在
     *
     * @param clientId
     */
    @Override
    public boolean checkClientId(String clientId) {
        Optional<ClientEntity> optionalClientEntity = clientRepository.findByClientId(clientId);
        ClientEntity clientEntity = optionalClientEntity.get();
        return clientEntity != null;
    }

    /**
     * 添加 auth code
     *
     * @param authCode
     * @param username
     */
    @Override
    public void addAuthCode(String authCode, String username) {

    }

    /**
     * 检查客户端安全Key是否正确
     *
     * @param clientSecret
     */
    @Override
    public boolean checkClientSecret(String clientSecret) {
        ClientEntity clientEntity = clientRepository.findByClientSecret(clientSecret);
        return clientEntity != null;
    }

    /**
     * 检查authCode是否可用
     *
     * @param authCode
     */
    @Override
    public boolean checkAuthCode(String authCode) {
        return true;
    }

    /**
     * 根据 authCode 获取用户名
     *
     * @param authCode
     */
    @Override
    public String getUsernameByAuthCode(String authCode) {
        return null;
    }

    /**
     * 添加accessToken
     *
     * @param accessToken
     * @param username
     */
    @Override
    public void addAccessToken(String accessToken, String username) {

    }

    /**
     * access token 过期时间
     */
    @Override
    public long getExpireIn() {
        return 0;
    }

    /**
     * 检查 accessToken 是否可用
     *
     * @param accessToken
     */
    @Override
    public boolean checkAccessToken(String accessToken) {

        return true;
    }

    /**
     * 根据 accessToken 获取用户名
     *
     * @param accessToken
     */
    @Override
    public String getUsernameByAccessToken(String accessToken) {
        return null;
    }
}
